package zcw.com.lib_leet_code;

import java.util.Arrays;

/**
 * Created by 朱城委 on 2021-7-17.<br><br>
 *
 * 分发饼干
 */
public class Topic455 {
    public static void main(String[] args) {
        Topic455 instance = new Topic455();

        int[] arrayG = new int[] {1,2,3};
        int[] arrayS = new int[] {1,1};
        System.out.println(instance.findContentChildren(arrayG, arrayS));

        arrayG = new int[] {1,2};
        arrayS = new int[] {1,2,3};
        System.out.println(instance.findContentChildren(arrayG, arrayS));
    }

    public int findContentChildren(int[] g, int[] s) {
        int result = 0;

        Arrays.sort(g);
        Arrays.sort(s);

        int indexG = g.length - 1;
        int indexS = s.length - 1;
        while (indexG >= 0 && indexS >= 0) {
            if(s[indexS] >= g[indexG]) {
                result++;
                indexS--;
            }
            indexG--;
        }

        return result;
    }

    public int findContentChildren2(int[] g, int[] s) {
        int result = 0;

        Arrays.sort(g);
        Arrays.sort(s);

        int indexG = 0;
        int indexS = 0;
        while (indexG < g.length && indexS < s.length) {
            if(s[indexS] >= g[indexG]) {
                result++;
                indexS++;
                indexG++;
            }
            else {
                indexS++;
            }
        }

        return result;
    }
} 
